<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>C++Qt | heit0ng</title><meta name="author" content="heit0ng"><meta name="copyright" content="heit0ng"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前言:学校事多，放假提前，课程堆一起了遭不住 QT跨平台图形界面引擎 优点:跨平台，接口简单容易上手，一定程度上简化了内存回收等 实例:Linux桌面环境KDE,谷歌地图，VLC多媒体播放器等 此处使用的是5.12版本Qt 后面具体案例就不整上来了，太麻烦了（懒.jpg） 创建第一个qt程序注意project的路径不能有中文出现  QWidget是QM与QD的父类 QW:创建出来就是个空的窗口 Q">
<meta property="og:type" content="article">
<meta property="og:title" content="C++Qt">
<meta property="og:url" content="https://heit0ng.gitee.io/2022/04/07/C++qt/index.html">
<meta property="og:site_name" content="heit0ng">
<meta property="og:description" content="前言:学校事多，放假提前，课程堆一起了遭不住 QT跨平台图形界面引擎 优点:跨平台，接口简单容易上手，一定程度上简化了内存回收等 实例:Linux桌面环境KDE,谷歌地图，VLC多媒体播放器等 此处使用的是5.12版本Qt 后面具体案例就不整上来了，太麻烦了（懒.jpg） 创建第一个qt程序注意project的路径不能有中文出现  QWidget是QM与QD的父类 QW:创建出来就是个空的窗口 Q">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s2.loli.net/2022/04/07/Cyg9WQLqSNxX7Av.png">
<meta property="article:published_time" content="2022-04-07T12:41:19.618Z">
<meta property="article:modified_time" content="2022-04-07T13:22:22.927Z">
<meta property="article:author" content="heit0ng">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2022/04/07/Cyg9WQLqSNxX7Av.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://heit0ng.gitee.io/2022/04/07/C++qt/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'C++Qt',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-04-07 21:22:22'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s2.loli.net/2022/01/28/RkXzVpjubg3yGEI.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div></div><hr/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s2.loli.net/2022/04/07/Cyg9WQLqSNxX7Av.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">heit0ng</a></span><div id="menus"><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">C++Qt</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-04-07T12:41:19.618Z" title="发表于 2022-04-07 20:41:19">2022-04-07</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-04-07T13:22:22.927Z" title="更新于 2022-04-07 21:22:22">2022-04-07</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="C++Qt"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>前言:学校事多，放假提前，课程堆一起了遭不住</p>
<p>QT跨平台图形界面引擎</p>
<p>优点:跨平台，接口简单容易上手，一定程度上简化了内存回收等</p>
<p>实例:Linux桌面环境KDE,谷歌地图，VLC多媒体播放器等</p>
<p>此处使用的是5.12版本Qt</p>
<p>后面具体案例就不整上来了，太麻烦了（懒.jpg）</p>
<h3 id="创建第一个qt程序"><a href="#创建第一个qt程序" class="headerlink" title="创建第一个qt程序"></a>创建第一个qt程序</h3><p>注意project的路径不能有中文出现</p>
<p><img src="https://s2.loli.net/2022/04/07/zBKIdNpTM8wn26k.png" alt="1号.png"></p>
<p>QWidget是QM与QD的父类</p>
<p>QW:创建出来就是个空的窗口</p>
<p>QM:多了菜单栏</p>
<p>QD:可以使用对话框，如图所示的东西也是对话框<img src="https://s2.loli.net/2022/04/07/kDxTzrWXov7sH2P.png" alt="2号.png"></p>
<p>版本控制系统一般在团队开发中用的更为广泛,如svn,vss,git</p>
<p>要添加的文件中.pro相当于.sln文件为一个工程文件</p>
<p>创建成功后的main中</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">#include &quot;widget.h&quot;</span><br><span class="line">#include &lt;QApplication&gt;//包含一个应用程序类的头文件</span><br><span class="line"></span><br><span class="line">//main程序入口</span><br><span class="line">int main(int argc, char *argv[])//argc，命令行变量的数量,argv命令行变量的数组</span><br><span class="line">&#123;</span><br><span class="line">    //a应用程序对象，在Qt中，应用冲虚对象，有且仅有一个</span><br><span class="line">    QApplication a(argc, argv);</span><br><span class="line">    //窗口对象 父类是QWidget Widget为派生类</span><br><span class="line">    //实例化窗口对象</span><br><span class="line">    Widget w;</span><br><span class="line">    //窗口对象 默认不会显示，必须要调用show函数显示窗口</span><br><span class="line">    w.show();</span><br><span class="line">    //让应用程序对象a进入消息循环机制</span><br><span class="line">    //让代码堵塞到这行，防止窗口一闪而过，直到进行操作，如点×退出</span><br><span class="line">    return a.exec();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="命名规范及快捷键"><a href="#命名规范及快捷键" class="headerlink" title="命名规范及快捷键"></a>命名规范及快捷键</h3><h4 id="pro文件的解释"><a href="#pro文件的解释" class="headerlink" title=".pro文件的解释"></a><strong>.pro文件的解释</strong></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">#-------------------------------------------------</span><br><span class="line">#</span><br><span class="line"># Project created by QtCreator 2022-03-23T19:54:40</span><br><span class="line">#</span><br><span class="line">#-------------------------------------------------</span><br><span class="line"></span><br><span class="line">QT       += core gui          Qt包含的模块core与gui</span><br><span class="line"></span><br><span class="line">greaterThan(QT_MAJOR_VERSION, 4): QT += widgets//greaterThan大于4版本以上，包含widget模块</span><br><span class="line"></span><br><span class="line">TARGET = first_project   //目标   生成的.exe名称，可以修改exe名称</span><br><span class="line">TEMPLATE = app	//模板  应用程序模板  Application，还有其他模板</span><br><span class="line"></span><br><span class="line"># The following define makes your compiler emit warnings if you use</span><br><span class="line"># any feature of Qt which has been marked as deprecated (the exact warnings</span><br><span class="line"># depend on your compiler). Please consult the documentation of the</span><br><span class="line"># deprecated API in order to know how to port your code away from it.</span><br><span class="line">DEFINES += QT_DEPRECATED_WARNINGS</span><br><span class="line"></span><br><span class="line"># You can also make your code fail to compile if you use deprecated APIs.</span><br><span class="line"># In order to do so, uncomment the following line.</span><br><span class="line"># You can also select to disable deprecated APIs only up to a certain version of Qt.</span><br><span class="line">#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0</span><br><span class="line"></span><br><span class="line">CONFIG += c++11</span><br><span class="line"></span><br><span class="line">SOURCES += \</span><br><span class="line">        main.cpp \   //源文件</span><br><span class="line">        widget.cpp   </span><br><span class="line"></span><br><span class="line">HEADERS += \    //头文件</span><br><span class="line">        widget.h</span><br><span class="line"></span><br><span class="line"># Default rules for deployment.</span><br><span class="line">qnx: target.path = /tmp/$$&#123;TARGET&#125;/bin</span><br><span class="line">else: unix:!android: target.path = /opt/$$&#123;TARGET&#125;/bin</span><br><span class="line">!isEmpty(target.path): INSTALLS += target</span><br></pre></td></tr></table></figure>



<p><img src="https://s2.loli.net/2022/04/07/UCgNbhxncMKByFt.png" alt="3号.png"></p>
<p>主要使用Widgets Core GUI模块</p>
<h4 id="头文件"><a href="#头文件" class="headerlink" title="头文件"></a>头文件</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">#ifndef WIDGET_H</span><br><span class="line">#define WIDGET_H</span><br><span class="line"></span><br><span class="line">#include &lt;QWidget&gt;//包含头文件QWidget窗口类</span><br><span class="line"></span><br><span class="line">class Widget : public QWidget</span><br><span class="line">&#123;</span><br><span class="line">    Q_OBJECT	//一个宏，允许类中使用信号和槽的机制</span><br><span class="line"></span><br><span class="line">public:</span><br><span class="line">    Widget(QWidget *parent = 0);  //构造函数，0为默认值</span><br><span class="line">    ~Widget();//析构函数</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">#endif // WIDGET_H</span><br></pre></td></tr></table></figure>

<h4 id="命名规范"><a href="#命名规范" class="headerlink" title="命名规范"></a>命名规范</h4><p>类名 首字母大写 ，单词和单词之间首字母大写</p>
<p>函数名 变量名称 首字母小写，单词和单词之间首字母大写</p>
<h4 id="快捷键"><a href="#快捷键" class="headerlink" title="快捷键"></a>快捷键</h4><p>注释ctrl + /</p>
<p>运行ctrl + r</p>
<p>编译ctrl + b</p>
<p>字体缩放ctrl + 鼠标滚轮</p>
<p>查找 ctrl + f</p>
<p>整行移动 ctrl + shift + ↑或者↓(复制粘贴都可以)</p>
<p>帮助文档F1 esc退出</p>
<p>自动对齐ctrl + i</p>
<p>同名之间的.h和.cpp切换 F4</p>
<h3 id="QPushButton创建"><a href="#QPushButton创建" class="headerlink" title="QPushButton创建"></a>QPushButton创建</h3><p>QPushButton帮助文档中</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">The QPushButton widget provides a command button. More...</span><br><span class="line"></span><br><span class="line">	Header:	#include &lt;QPushButton&gt; //头文件</span><br><span class="line">	qmake:	QT += widgets //需要的模块，在.pro中修改</span><br><span class="line">	Inherits:	QAbstractButton  //父类</span><br><span class="line">	Inherited By:	QCommandLinkButton  //子类</span><br><span class="line"></span><br><span class="line">List of all members, including inherited members</span><br><span class="line">Obsolete members </span><br></pre></td></tr></table></figure>

<p>创建按钮</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">#include &quot;widget.h&quot;</span><br><span class="line">#include&lt;QPushButton&gt;//按钮控件的头文件</span><br><span class="line"></span><br><span class="line">Widget::Widget(QWidget *parent)</span><br><span class="line">    : QWidget(parent)</span><br><span class="line">&#123;</span><br><span class="line">    //创建一个按钮</span><br><span class="line">    //他的父类的父类是widget，要用show显示</span><br><span class="line">    QPushButton * btn = new QPushButton;</span><br><span class="line">    //show的显示方式是用顶层方式，单独用一个窗口控件</span><br><span class="line">    //btn-&gt;show();</span><br><span class="line">    //让btn对象依赖在widget中</span><br><span class="line">    btn-&gt;setParent(this);</span><br><span class="line">    </span><br><span class="line">    //显示 文本</span><br><span class="line">    btn-&gt;setText(&quot;第一个按钮&quot;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Widget::~Widget()</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://s2.loli.net/2022/04/07/KGHCbu4B9oweOt2.png" alt="4号.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">#include &quot;widget.h&quot;</span><br><span class="line">#include&lt;QPushButton&gt;//按钮控件的头文件</span><br><span class="line"></span><br><span class="line">Widget::Widget(QWidget *parent)</span><br><span class="line">    : QWidget(parent)</span><br><span class="line">&#123;</span><br><span class="line">    //创建一个按钮</span><br><span class="line">    //他的父类的父类是widget，要用show显示</span><br><span class="line">    QPushButton * btn = new QPushButton;</span><br><span class="line">    //show的显示方式是用顶层方式，单独用一个窗口控件</span><br><span class="line">    //btn-&gt;show();</span><br><span class="line">    //让btn对象依赖在widget中</span><br><span class="line">    btn-&gt;setParent(this);</span><br><span class="line"></span><br><span class="line">    //显示 文本</span><br><span class="line">    btn-&gt;setText(&quot;第一个按钮&quot;);</span><br><span class="line"></span><br><span class="line">    //创建第二个按钮，按s照控件的大小创建窗口</span><br><span class="line">    QPushButton * btn2 = new QPushButton(&quot;第二个按钮&quot;,this);//文本内容，负载体</span><br><span class="line"></span><br><span class="line">    //移动btn2按钮</span><br><span class="line">    btn2-&gt;move(100,100);//参数长与宽</span><br><span class="line"></span><br><span class="line">    //重置窗口大小</span><br><span class="line">    resize(600,400);</span><br><span class="line"></span><br><span class="line">	//按钮大小</span><br><span class="line">	btn2-&gt;resize(50,50);</span><br><span class="line">	</span><br><span class="line">    //设置固定窗口大小</span><br><span class="line">    setFixedSize(600,400);</span><br><span class="line">    //设置窗口标题</span><br><span class="line">    setWindowTitle(&quot;第一个窗口&quot;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Widget::~Widget()</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://s2.loli.net/2022/04/07/9JQnFu145MBxPkm.png" alt="5号.png"></p>
<p>如果中文乱码查看工具–行为里面编码改为utf-8</p>
<h3 id="对象树"><a href="#对象树" class="headerlink" title="对象树"></a>对象树</h3><p>当创建的对象在堆区时候，如果指定的父亲是QObject派生下来的类或者QObject子类派生下来的类可以不用管理释放的操作，将对象放入到对象树中，一定程度上简化了内存回收机制</p>
<p>QWidget是能够在屏幕上显示的一切组件的父类<img src="https://s2.loli.net/2022/04/07/u1kVRUQD4lCYxWE.png" alt="6号.png"></p>
<p>构造时从上往下，析构时从下往上</p>
<p>qDebug用于在控制台输出调试信息，主要有以下几种用法。</p>
<p>1、类似c++的cout函数</p>
<p>2、构造函数直接引入参数</p>
<p><img src="https://s2.loli.net/2022/04/07/ILK9DP3YhieF1X6.png" alt="7号.png"></p>
<p>添加一个文件，选择C++ Class继承的是QPushButton</p>
<p><img src="https://s2.loli.net/2022/04/07/RzGuyKrZBI8Ago2.png" alt="8号.png"></p>
<p>可以添加自己的头文件</p>
<p><img src="https://s2.loli.net/2022/04/07/g6y2CImn3E5zD9a.png" alt="9号.png"></p>
<h3 id="信号和槽"><a href="#信号和槽" class="headerlink" title="信号和槽"></a>信号和槽</h3><h3 id=""><a href="#" class="headerlink" title=""></a><img src="https://s2.loli.net/2022/04/07/lgc3dtX2YwPz4Bh.png" alt="10号.png"></h3><h4 id="连接函数connect"><a href="#连接函数connect" class="headerlink" title="连接函数connect"></a>连接函数connect</h4><p>人摩擦神灯出现灯神，这是一个过程，就像点击窗口的按钮，就可以关闭窗口，人是信号发送者，神灯是接收信号的</p>
<p>但人摩擦神灯一定会出现灯神吗？不一定。信号槽就把信号发送端和信号接受端连接了起来</p>
<p>关于信号</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Signals</span><br><span class="line"></span><br><span class="line">void clicked(bool checked = false)</span><br><span class="line">void pressed()</span><br><span class="line">void released()</span><br><span class="line">void toggled(bool checked)//状态切换，按下去与抬起来</span><br><span class="line"></span><br><span class="line">3 signals inherited from QWidget</span><br><span class="line">2 signals inherited from QObject </span><br></pre></td></tr></table></figure>

<p>关于槽函数可以在帮助文档里面查</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">~~~上略</span><br><span class="line">//需求 点击我的按钮 关闭窗口</span><br><span class="line">   //参数1 信号的发送者 参数2 发送的信号(函数的地址) 参数3 信号接收者 参数4 处理的槽函数</span><br><span class="line">   connect(myBtn,&amp;QPushButton::clicked,this,&amp;Widget::close );//点击我的按钮可以关闭窗口</span><br></pre></td></tr></table></figure>

<h4 id="自定义信号和槽"><a href="#自定义信号和槽" class="headerlink" title="自定义信号和槽"></a>自定义信号和槽</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">自定义信号</span><br><span class="line">	返回void，只需声明不需实现，可以有参数(重载)；写到signals 下</span><br><span class="line">自定义槽函数</span><br><span class="line">	返回void，要声明并实现，可以有参数(重载)；写到public下/public solt 下</span><br><span class="line">自定义触发信号</span><br><span class="line">	emit 自定义信号</span><br><span class="line">槽函数重载注意事项</span><br><span class="line">	要用函数指针明确指向函数的地址，类Teacher的成员函数 void hungry(QSting)；其函数指针可写为：void(Teacher::myfunSignal)(Qstring)=&amp;Teacher::hungry;</span><br><span class="line">	</span><br><span class="line">QString转成char*</span><br><span class="line">	ToUtf8()转成QByteArray</span><br><span class="line">	Data()转成Char*</span><br></pre></td></tr></table></figure>

<h4 id="信号连接"><a href="#信号连接" class="headerlink" title="信号连接"></a>信号连接</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">![11号](D:\learn\笔记\QT\C++QT\11号.png#include &quot;widget.h&quot;</span><br><span class="line">#include &quot;ui_widget.h&quot;</span><br><span class="line">#include&lt;QPushButton&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">//Teacher 类</span><br><span class="line">//Student 类</span><br><span class="line">//下课后，老师会触发一个信号，饿了学生响应信号，请客吃饭</span><br><span class="line"></span><br><span class="line">Widget::Widget(QWidget *parent) :</span><br><span class="line">    QWidget(parent),</span><br><span class="line">    ui(new Ui::Widget)</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    ui-&gt;setupUi(this);</span><br><span class="line"></span><br><span class="line">    //创建一个老师对象</span><br><span class="line">    this-&gt;zt = new Teacher(this);</span><br><span class="line"></span><br><span class="line">    //创建一个学生对象</span><br><span class="line">    this-&gt;st = new Student(this);</span><br><span class="line">//    //老师饿了，学生请客连接</span><br><span class="line">//    connect(zt,&amp;Teacher::hungry,st,&amp;Student::treat);</span><br><span class="line"></span><br><span class="line">//    //调用下课函数</span><br><span class="line">//    classIsOver();</span><br><span class="line">    //链接带参数的信号和槽</span><br><span class="line">    //地址-&gt;地址</span><br><span class="line">    //函数指针-&gt;函数地址</span><br><span class="line">    void (Teacher::*teacherSignal)(QString)= &amp;Teacher::hungry;</span><br><span class="line">    void (Student::*studentSlot)(QString)= &amp;Student::treat;</span><br><span class="line">    connect(zt,teacherSignal,st,studentSlot);</span><br><span class="line">    //classIsOver();</span><br><span class="line"></span><br><span class="line">    //点击一个按钮再下课</span><br><span class="line">    QPushButton * btn = new QPushButton(&quot;下课&quot;,this);</span><br><span class="line"></span><br><span class="line">    //重置窗口大小</span><br><span class="line">    this-&gt;resize(600,400);</span><br><span class="line"></span><br><span class="line">    //点击按钮触发下课</span><br><span class="line">    //connect(btn,&amp;QPushButton::clicked,this,&amp;Widget::classIsOver);</span><br><span class="line"></span><br><span class="line">    //无参信合和槽连接</span><br><span class="line">    void (Teacher::*teacherSignal2)(void)= &amp;Teacher::hungry;</span><br><span class="line">    void (Student::*studentSlot2)(void)= &amp;Student::treat;</span><br><span class="line">    connect(zt,teacherSignal2,st,studentSlot2);</span><br><span class="line"></span><br><span class="line">    //信号连接信号</span><br><span class="line">    connect(btn,&amp;QPushButton::clicked,zt,teacherSignal2);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">void Widget::classIsOver()</span><br><span class="line">&#123;</span><br><span class="line">    //下课函数，调用触发老师饿了的信号</span><br><span class="line">    //emit zt-&gt;hungry();</span><br><span class="line">    emit zt-&gt;hungry(&quot;宫保鸡丁&quot;);</span><br><span class="line">&#125;</span><br><span class="line">Widget::~Widget()</span><br><span class="line">&#123;</span><br><span class="line">    delete ui;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://s2.loli.net/2022/04/07/8zvrd4FcYqMUkai.png" alt="11号.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">//断开信号</span><br><span class="line">   disconnect(zt,teacherSignal2,st,studentSlot2);</span><br><span class="line">   参数和disconnect相同</span><br><span class="line">   此处将老师信号与学生请客间断开</span><br></pre></td></tr></table></figure>

<h3 id="Lambda表达式"><a href="#Lambda表达式" class="headerlink" title="Lambda表达式"></a>Lambda表达式</h3><p>Lambda 表达式的定义形式如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[函数对象参数] (操作符重载函数参数) -&gt; 返回值（函数体）</span><br><span class="line"> &#123;</span><br><span class="line">  语句块</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<p>[ ]这部分必须存在不能忽略，标识Lambda的开始，函数对象参数是传递给编译器自动生成的函数对象类的构造函数的，函数对象参数只能使用那些定义到Lambda所在作用范围内可见的局部变量</p>
<h4 id="函数对象参数形式"><a href="#函数对象参数形式" class="headerlink" title="函数对象参数形式:"></a>函数对象参数形式:</h4><p>空。没有使用任何函数对象参数</p>
<p>=。函数体内可以使用Lambda所在作用范围内可见的局部变量，并且是值传递方式</p>
<p>&amp;。函数体内可以使用Lambda所在作用范围内可见的局部变量，并且是引用传递方式</p>
<p>this。函数体内可以使用Lambda所在类中的成员变量</p>
<p>a。将a按值进行传递，按值进行传递时。函数体内不能修改传递进来的a的拷贝</p>
<p>&amp;a。将a按引用进行传递</p>
<p>a，&amp;b。将a按值进行传递，b按引用进行传递</p>
<p>=，&amp;a，&amp;b 除a和b按引用进行传递歪其他都按值</p>
<p>&amp;，a,b。除了a和b按值进行外其他参数都按引用进行传递</p>
<h4 id="操作重载函数"><a href="#操作重载函数" class="headerlink" title="操作重载函数"></a>操作重载函数</h4><p>没有参数时这部分可以省略，参数可以通过按值（（a,b））和按引用((&amp;a,&amp;b))的方式进行传递</p>
<h4 id="可修改表示符"><a href="#可修改表示符" class="headerlink" title="可修改表示符"></a>可修改表示符</h4><p>mutable声明 这部分可以省略。按值传递函数对象参数时，加上mutable修饰符后，可以修改按值传递进来的 <strong>拷贝</strong>不能修改本体</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">QPushButton * myBtn= new QPushButton (this);</span><br><span class="line">   QPushButton * myBtn2 = new QPushButton(this);</span><br><span class="line">   </span><br><span class="line">   myBtn2-&gt;move(100,100);</span><br><span class="line">   </span><br><span class="line">   int m=10;</span><br><span class="line">   </span><br><span class="line">   connect(myBtn,&amp;QPushButton::clicked,this,[m] ()mutable&#123;m=100+10;qDebug()&lt;&lt;m;&#125;);</span><br><span class="line">   </span><br><span class="line">   connect(myBtn2,&amp;QPushButton::clicked,this,[=] ()&#123;qDebug()&lt;&lt;m;&#125;);</span><br><span class="line">   </span><br><span class="line">   qDebug()&lt;&lt;m;</span><br></pre></td></tr></table></figure>

<h4 id="函数返回值"><a href="#函数返回值" class="headerlink" title="函数返回值"></a>函数返回值</h4><p>-&gt;返回值类型，标识函数返回值的类型，当返回值为void，或者函数体重只有一处return的地方（此时编译器可以自动推断出返回值的类型）时，这部分可以省略</p>
<h4 id="函数体"><a href="#函数体" class="headerlink" title="函数体"></a>函数体</h4><p>{}，标识函数的实现，这部分不能省略，但函数体可以为空</p>
<p>实例:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">//利用Lambda表达式 实现点击按钮关闭窗口</span><br><span class="line">    QPushButton * btn2 = new QPushButton;</span><br><span class="line">    btn2-&gt;setText(&quot;关闭&quot;);</span><br><span class="line">    btn2-&gt;move(100,0);</span><br><span class="line">    btn2-&gt;setParent(this);</span><br><span class="line">    connect(btn2,&amp;QPushButton::clicked,this,[=]()&#123;</span><br><span class="line">        this-&gt;close();</span><br><span class="line">        emit zt-&gt;hungry(&quot;宫保鸡丁&quot;);</span><br><span class="line">    &#125;);</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lambda表达式最常用[=]()&#123;&#125;</span><br></pre></td></tr></table></figure>

<h3 id="QMainWindow"><a href="#QMainWindow" class="headerlink" title="QMainWindow"></a>QMainWindow</h3><p>QMainWindow是一个为用户提供主窗口程序的类，包含一个菜单栏（menu bar）、多个工具栏(tool bars)、多个锚接部件(dock widgets)、一个状态栏(status bar)及一个中心部件(central widget)，是许多应用程序的基础，如文本编辑器，图片编辑器等。</p>
<h4 id="菜单栏-QMenuBar"><a href="#菜单栏-QMenuBar" class="headerlink" title="菜单栏 QMenuBar"></a>菜单栏 QMenuBar</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">菜单栏最多只能有一个</span><br><span class="line">     QMenuBar * bar = menuBar(); setMenuBar(bar);</span><br><span class="line">     创建菜单</span><br><span class="line">         QMenu * fileMenu = bar-&gt;addMenu(&quot;文件&quot;);</span><br><span class="line">             创建菜单栏目</span><br><span class="line">                * QAction * newAction =  fileMenu-&gt;addAction(&quot;新建&quot;);</span><br><span class="line">             添加分隔符 </span><br><span class="line">                * fileMenu-&gt;addSeparator();</span><br></pre></td></tr></table></figure>

<h4 id="工具栏-QToolBar"><a href="#工具栏-QToolBar" class="headerlink" title="工具栏 QToolBar"></a>工具栏 QToolBar</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">工具栏可以有多个</span><br><span class="line">    QToolBar * toolBar = new QToolBar(this);  </span><br><span class="line">    addToolBar(toolBar);</span><br><span class="line">       可选参数 默认停靠范围</span><br><span class="line">           + addToolBar(Qt::BottomToolBarArea,toolBar);</span><br><span class="line">       只允许左右停靠</span><br><span class="line">           + toolBar-&gt;setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);</span><br><span class="line">       取消浮动</span><br><span class="line">           + toolBar-&gt;setFloatable(false);</span><br><span class="line">       设置禁止移动</span><br><span class="line">           + toolBar-&gt;setMovable(false);</span><br><span class="line">       给工具栏设置栏目</span><br><span class="line">           + toolBar-&gt;addAction(&quot;绝了&quot;或者QAction);</span><br><span class="line">       给工具栏添加控件</span><br><span class="line">           + toolBar-&gt;addWidget(QPushButton按钮);</span><br></pre></td></tr></table></figure>

<h4 id="状态栏-QStatusBar"><a href="#状态栏-QStatusBar" class="headerlink" title="状态栏 QStatusBar"></a>状态栏 QStatusBar</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">* 状态栏最多只能有一个</span><br><span class="line">    QStatusBar * stBar = statusBar();</span><br><span class="line">    setStatusBar(stBar);</span><br><span class="line">        * 添加标签控件</span><br><span class="line">            + QLabel * label = new QLabel(&quot;左侧提示的信息&quot;,this);</span><br><span class="line">            + QLabel * label1 = new QLabel(&quot;右侧提示的信息&quot;,this);</span><br><span class="line">            + stBar-&gt;addWidget(label);</span><br><span class="line">            + stBar-&gt;addPermanentWidget(label1);</span><br></pre></td></tr></table></figure>

<h4 id="铆接部件-浮动窗口-QDockWidget"><a href="#铆接部件-浮动窗口-QDockWidget" class="headerlink" title="铆接部件(浮动窗口) QDockWidget"></a>铆接部件(浮动窗口) QDockWidget</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">* 铆接部件可以有多个</span><br><span class="line">    + QDockWidget * dockWidget = new QDockWidget(&quot;浮动&quot;,this);</span><br><span class="line">    + addDockWidget(Qt::BottomDockWidgetArea,dockWidget); 放置位置下面 如果没有中心部件默认占满</span><br><span class="line">        + 只允许上下</span><br><span class="line">                * dockWidget-&gt;setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);  </span><br></pre></td></tr></table></figure>

<h4 id="中心部件"><a href="#中心部件" class="headerlink" title="中心部件"></a>中心部件</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">* 中心内容也只能有一个</span><br><span class="line">    + 文本窗口 QTextEdit</span><br><span class="line">        + QTextEdit * edit = new QTextEdit(this);</span><br><span class="line">        + setCentralWidget(edit); //设置中心部件</span><br></pre></td></tr></table></figure>

<h4 id="资源文件添加"><a href="#资源文件添加" class="headerlink" title="资源文件添加"></a>资源文件添加</h4><ol>
<li>将图片文件文件夹拷贝到项目下</li>
<li>右键项目-&gt;添加新文件-&gt;Qt-&gt;Qt recourse File</li>
<li>res 生成 res.qrc</li>
<li>右键res.qrc-&gt;open in editor 编辑资源</li>
<li>添加前缀 添加文件</li>
<li>使用 “: + 前缀名 + 文件名”</li>
</ol>
<p>总结 只能有一个的为set 可以有多个的为add</p>
<h3 id="对话框"><a href="#对话框" class="headerlink" title="对话框"></a>对话框</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">模态对话框 不可以对其他窗口进行操作</span><br><span class="line">        QDialog dlg(this);</span><br><span class="line">        dlg.exec();</span><br><span class="line">        消息对话框</span><br><span class="line">            错误对话框 QMessageBox::critical(this,&quot;critical&quot;,&quot;错误&quot;);</span><br><span class="line">            信息对话框 information</span><br><span class="line">            提问对话框 question</span><br><span class="line">             参数一 父亲 参数2 标题 参数三 提示内容 参数四 按键类型 参数5 默认关联回车按键</span><br><span class="line">          if(QMessageBox::Save == QMessageBox::question(this,&quot;question&quot;,&quot;提		  问&quot;,QMessageBox::Save|QMessageBox::Cancel,QMessageBox::Cancel))</span><br><span class="line">          &#123;</span><br><span class="line">              qDebug()&lt;&lt;&quot;选择的是保存&quot;;</span><br><span class="line">          &#125;</span><br><span class="line">          else</span><br><span class="line">          &#123;</span><br><span class="line">              qDebug()&lt;&lt;&quot;选择的是取消&quot;;</span><br><span class="line">          &#125;</span><br><span class="line">            警告对话框 warning</span><br><span class="line">            返回值是standarbutton类型，利用返回值判断用户输入</span><br><span class="line">            </span><br><span class="line">            颜色对话框</span><br><span class="line">                QColor a = QColorDialog::getColor(QColor(255,0,0));</span><br><span class="line">            文件对话框 最后一个是过滤</span><br><span class="line">                QString str = QFileDialog::getOpenFileName(this,&quot;打开文件&quot;,&quot;./&quot;,&quot;(*.cpp)&quot;);</span><br><span class="line">                QString str =  QFileDialog::getOpenFileName(this,&quot;打开文件&quot;,&quot;C:\\Users\\19859\\Desktop&quot;,&quot;(*.txt)&quot;);</span><br><span class="line">            字体对话框</span><br><span class="line">                bool flag;</span><br><span class="line">                QFont font = QFontDialog::getFont(&amp;flag,QFont(&quot;华文彩云&quot;,12));</span><br><span class="line">                setFont(font);//设置字体</span><br><span class="line">        bool flag;</span><br><span class="line">        QFont font = QFontDialog::getFont(&amp;flag,QFont(&quot;华文彩云&quot;,36));</span><br><span class="line">        qDebug()&lt;&lt;&quot;字体: &quot;&lt;&lt;font.family()&lt;&lt;&quot;字号&quot;&lt;&lt;font.pointSize()&lt;&lt;&quot;是否加粗&quot;&lt;&lt;font.bold()&lt;&lt;&quot;是否倾斜&quot;&lt;&lt;font.italic();</span><br><span class="line">                </span><br><span class="line">    非模态对话框 可以对其他窗口进行操作</span><br><span class="line">        QDialog *dlg2 = new QDialog(this); //为了确保不释放,开在堆上</span><br><span class="line">        dlg2-&gt;show();</span><br><span class="line">        dlg2-&gt;setAttribute(Qt::WA_DeleteOnClose);//55号 用于按关闭键自动释放[QWidge的对象树是在关闭总的窗口才会全部释放]</span><br></pre></td></tr></table></figure>

<p>UI界面布局和控件这里就不介绍了（麻烦，可以自己去查怎么使用）</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">heit0ng</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://heit0ng.gitee.io/2022/04/07/C++qt/">https://heit0ng.gitee.io/2022/04/07/C++qt/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://heit0ng.gitee.io" target="_blank">heit0ng</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://s2.loli.net/2022/04/07/Cyg9WQLqSNxX7Av.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/"><img class="prev-cover" src="https://s2.loli.net/2022/05/16/ae4E7n2D3I5mqkh.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">C语言图书管理系统</div></div></a></div><div class="next-post pull-right"><a href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/"><img class="next-cover" src="https://s2.loli.net/2022/03/12/QrwRKYfGEmP3vVL.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">c++的知识(1)</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s2.loli.net/2022/01/28/RkXzVpjubg3yGEI.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">heit0ng</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E7%AC%AC%E4%B8%80%E4%B8%AAqt%E7%A8%8B%E5%BA%8F"><span class="toc-number">1.</span> <span class="toc-text">创建第一个qt程序</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%91%BD%E5%90%8D%E8%A7%84%E8%8C%83%E5%8F%8A%E5%BF%AB%E6%8D%B7%E9%94%AE"><span class="toc-number">2.</span> <span class="toc-text">命名规范及快捷键</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#pro%E6%96%87%E4%BB%B6%E7%9A%84%E8%A7%A3%E9%87%8A"><span class="toc-number">2.1.</span> <span class="toc-text">.pro文件的解释</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A4%B4%E6%96%87%E4%BB%B6"><span class="toc-number">2.2.</span> <span class="toc-text">头文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%91%BD%E5%90%8D%E8%A7%84%E8%8C%83"><span class="toc-number">2.3.</span> <span class="toc-text">命名规范</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BF%AB%E6%8D%B7%E9%94%AE"><span class="toc-number">2.4.</span> <span class="toc-text">快捷键</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#QPushButton%E5%88%9B%E5%BB%BA"><span class="toc-number">3.</span> <span class="toc-text">QPushButton创建</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E6%A0%91"><span class="toc-number">4.</span> <span class="toc-text">对象树</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%E5%92%8C%E6%A7%BD"><span class="toc-number">5.</span> <span class="toc-text">信号和槽</span></a></li><li class="toc-item toc-level-3"><a class="toc-link"><span class="toc-number">6.</span> <span class="toc-text"></span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5%E5%87%BD%E6%95%B0connect"><span class="toc-number">6.1.</span> <span class="toc-text">连接函数connect</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E4%BF%A1%E5%8F%B7%E5%92%8C%E6%A7%BD"><span class="toc-number">6.2.</span> <span class="toc-text">自定义信号和槽</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%E8%BF%9E%E6%8E%A5"><span class="toc-number">6.3.</span> <span class="toc-text">信号连接</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">7.</span> <span class="toc-text">Lambda表达式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%AF%B9%E8%B1%A1%E5%8F%82%E6%95%B0%E5%BD%A2%E5%BC%8F"><span class="toc-number">7.1.</span> <span class="toc-text">函数对象参数形式:</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C%E9%87%8D%E8%BD%BD%E5%87%BD%E6%95%B0"><span class="toc-number">7.2.</span> <span class="toc-text">操作重载函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%AF%E4%BF%AE%E6%94%B9%E8%A1%A8%E7%A4%BA%E7%AC%A6"><span class="toc-number">7.3.</span> <span class="toc-text">可修改表示符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-number">7.4.</span> <span class="toc-text">函数返回值</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E4%BD%93"><span class="toc-number">7.5.</span> <span class="toc-text">函数体</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#QMainWindow"><span class="toc-number">8.</span> <span class="toc-text">QMainWindow</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%8F%9C%E5%8D%95%E6%A0%8F-QMenuBar"><span class="toc-number">8.1.</span> <span class="toc-text">菜单栏 QMenuBar</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B7%A5%E5%85%B7%E6%A0%8F-QToolBar"><span class="toc-number">8.2.</span> <span class="toc-text">工具栏 QToolBar</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%8A%B6%E6%80%81%E6%A0%8F-QStatusBar"><span class="toc-number">8.3.</span> <span class="toc-text">状态栏 QStatusBar</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%93%86%E6%8E%A5%E9%83%A8%E4%BB%B6-%E6%B5%AE%E5%8A%A8%E7%AA%97%E5%8F%A3-QDockWidget"><span class="toc-number">8.4.</span> <span class="toc-text">铆接部件(浮动窗口) QDockWidget</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%AD%E5%BF%83%E9%83%A8%E4%BB%B6"><span class="toc-number">8.5.</span> <span class="toc-text">中心部件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B5%84%E6%BA%90%E6%96%87%E4%BB%B6%E6%B7%BB%E5%8A%A0"><span class="toc-number">8.6.</span> <span class="toc-text">资源文件添加</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%B9%E8%AF%9D%E6%A1%86"><span class="toc-number">9.</span> <span class="toc-text">对话框</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/03/22/j2ee(2)/" title="j2ee(2)"><img src="https://s2.loli.net/2023/03/23/mTDuOeL67MFtgZs.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="j2ee(2)"/></a><div class="content"><a class="title" href="/2023/03/22/j2ee(2)/" title="j2ee(2)">j2ee(2)</a><time datetime="2023-03-22T15:50:01.860Z" title="发表于 2023-03-22 23:50:01">2023-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/22/j2ee(1)/" title="j2ee(1)"><img src="https://s2.loli.net/2023/03/22/3KqCOLtHBdvhMFe.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="j2ee(1)"/></a><div class="content"><a class="title" href="/2023/03/22/j2ee(1)/" title="j2ee(1)">j2ee(1)</a><time datetime="2023-03-22T12:20:17.683Z" title="发表于 2023-03-22 20:20:17">2023-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="C语言图书管理系统"><img src="https://s2.loli.net/2022/05/16/ae4E7n2D3I5mqkh.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C语言图书管理系统"/></a><div class="content"><a class="title" href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="C语言图书管理系统">C语言图书管理系统</a><time datetime="2022-05-16T14:28:31.730Z" title="发表于 2022-05-16 22:28:31">2022-05-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/04/07/C++qt/" title="C++Qt"><img src="https://s2.loli.net/2022/04/07/Cyg9WQLqSNxX7Av.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C++Qt"/></a><div class="content"><a class="title" href="/2022/04/07/C++qt/" title="C++Qt">C++Qt</a><time datetime="2022-04-07T12:41:19.618Z" title="发表于 2022-04-07 20:41:19">2022-04-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/" title="c++的知识(1)"><img src="https://s2.loli.net/2022/03/12/QrwRKYfGEmP3vVL.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="c++的知识(1)"/></a><div class="content"><a class="title" href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/" title="c++的知识(1)">c++的知识(1)</a><time datetime="2022-03-12T13:48:26.310Z" title="发表于 2022-03-12 21:48:26">2022-03-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By heit0ng</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>